//
//  RootViewController.m
//  Backhoe
//
//  Created by Jeremy Przasnyski on 11/15/08.
//  Copyright __MyCompanyName__ 2008. All rights reserved.
//

#import "RootViewController.h"
#import "BackhoeAppDelegate.h"
#import "StoryDetailController.h"

@interface RootViewController ()
@property (nonatomic, retain) StoryDetailController *storyDetailController;
@end

@implementation RootViewController

@synthesize storyDetailController;

- (void)awakeFromNib {
	stories = [[NSArray alloc] init];
}

- (StoryDetailController*) storyDetailController {
    // Instantiate the detail view controller if necessary.
    if (storyDetailController == nil) {
        storyDetailController = [[StoryDetailController alloc] initWithNibName:@"StoryDetail" bundle:nil];
    }
    return storyDetailController;
}

- (IBAction)refreshClicked:(id)sender {
	BackhoeAppDelegate *appDelegate = (BackhoeAppDelegate *)[[UIApplication sharedApplication] delegate];
	stories = [appDelegate.diggStoryBridge getLatestStories];
	[((UITableView*)self.view) reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [stories count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
    }
	cell.text = ((DiggStory*)[stories objectAtIndex:[indexPath row]]).title;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
		StoryDetailController *controller = self.storyDetailController;
		controller.story = [stories objectAtIndex:indexPath.row];
    [self.navigationController pushViewController:controller animated:YES];
}



- (void)viewDidLoad {
    [super viewDidLoad];
	UIBarButtonItem *refreshButton = [[UIBarButtonItem alloc] initWithTitle:@"Refresh"
									style:UIBarButtonItemStyleBordered target:self action:@selector(refreshClicked:)];
	self.navigationItem.rightBarButtonItem = refreshButton;
		self.title = @"Digg - Backhoe";
}


/*
// Override to support editing the list
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support conditional editing of the list
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support rearranging the list
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the list
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
}
*/

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
    // Release anything that's not essential, such as cached data
}


- (void)dealloc {
    [super dealloc];
}


@end

